unit UNewSchemeWiz;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UBaseWizard, StdCtrls, ExtCtrls, ComCtrls, ConceptualSchemes,
  BourbakiLogic, UBaseModalForm, ActnList;

type
  TNewSchemeWizard = class(TBaseWizard)
    Page1: TTabSheet;
    Label1: TLabel;
    rgSchemeType: TRadioGroup;
    Page2: TTabSheet;
    lbCS1: TListBox;
    lbBaseSets: TListBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Page3: TTabSheet;
    Label5: TLabel;
    Label6: TLabel;
    lbCS2: TListBox;
    lbConstituents: TListBox;
    Label7: TLabel;
    Page4: TTabSheet;
    Label8: TLabel;
    Label9: TLabel;
    lbCS3: TListBox;
    lbStructures: TListBox;
    Label10: TLabel;
    procedure bBackClick(Sender: TObject);
    procedure lbCS1Click(Sender: TObject);
    procedure acDoneUpdate(Sender: TObject);
    procedure acBackwardUpdate(Sender: TObject);
    procedure acForwardUpdate(Sender: TObject);
    procedure acForwardExecute(Sender: TObject);
    procedure acBackwardExecute(Sender: TObject);
    procedure lbCS2Click(Sender: TObject);
    procedure lbCS3Click(Sender: TObject);
  end;
function SetupNewScheme(Kernel: TBourbakiDoc): TConceptualScheme;
implementation

{$R *.dfm}

function SetupNewScheme(Kernel: TBourbakiDoc): TConceptualScheme;
var
  i: Integer;
begin
  with TNewSchemeWizard.Create(nil) do
  try
    for i := 0 to Pred(Kernel.SchemeCount) do
    begin
      lbCS1.AddItem(Kernel.Scheme[i].Caption, Kernel.Scheme[i]);
      lbCS2.AddItem(Kernel.Scheme[i].Caption, Kernel.Scheme[i]);
      lbCS3.AddItem(Kernel.Scheme[i].Caption, Kernel.Scheme[i]);
    end;
    WizardPages.ActivePage := Page1;
    ShowModal;

    if ModalResult = mrOK then
      case rgSchemeType.ItemIndex of
        0: Result := Kernel.AddScheme;
        1: Result := Kernel.AddScheme(
            TConceptualScheme(lbCS1.Items.Objects[lbCS1.ItemIndex]),
            TConceptualScheme(lbCS2.Items.Objects[lbCS2.ItemIndex]),
            lbBaseSets.Items[lbBaseSets.ItemIndex],
            lbConstituents.Items[lbConstituents.ItemIndex]);
        2: Result := Kernel.AddScheme(
            TConceptualScheme(lbCS3.Items.Objects[lbCS3.ItemIndex]),
            lbStructures.Items[lbStructures.ItemIndex]);
      else
        Result := nil;
      end
    else
      Result := nil;

  finally
    Free;
  end;

end;

procedure TNewSchemeWizard.bBackClick(Sender: TObject);
begin
  inherited;
  if (WizardPages.ActivePage = Page2)
    or (WizardPages.ActivePage = Page4) then
  begin
    WizardPages.ActivePage := Page1;
    bForward.Enabled := True;
    bBack.Enabled := False;
  end;
end;

procedure TNewSchemeWizard.lbCS1Click(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  lbBaseSets.Clear;
  with TConceptualScheme(lbCS1.Items.Objects[lbCS1.ItemIndex]) do
  begin
    for i := 0 to Pred(ConstituentsCount) do
    begin
      if Constituents[i].ConstituentType = ctBaseSet then
        lbBaseSets.AddItem(Constituents[i].CName, Constituents[i]);
    end;
  end;
end;

procedure TNewSchemeWizard.acDoneUpdate(Sender: TObject);
begin
  inherited;
  if WizardPages.ActivePage = Page1 then
    acDone.Enabled := rgSchemeType.ItemIndex = 0
  else if WizardPages.ActivePage = Page3 then
    acDone.Enabled := lbConstituents.ItemIndex >= 0
  else if WizardPages.ActivePage = Page4 then
    acDone.Enabled := lbStructures.ItemIndex >= 0
  else
    acDone.Enabled := False;
end;

procedure TNewSchemeWizard.acBackwardUpdate(Sender: TObject);
begin
  inherited;
  acBackward.Enabled := WizardPages.ActivePage <> Page1;
end;

procedure TNewSchemeWizard.acForwardUpdate(Sender: TObject);
begin
  inherited;
  if WizardPages.ActivePage = Page1 then
    acForward.Enabled := rgSchemeType.ItemIndex > 0
  else if WizardPages.ActivePage = Page2 then
    acForward.Enabled := lbBaseSets.ItemIndex >= 0
  else
    acForward.Enabled := False;

end;

procedure TNewSchemeWizard.acForwardExecute(Sender: TObject);
begin
  inherited;
  if WizardPages.ActivePage = Page1 then
  begin
    if rgSchemeType.ItemIndex = 1 then
      WizardPages.ActivePage := Page2
    else
      WizardPages.ActivePage := Page4;
  end
  else if WizardPages.ActivePage = Page2 then
    WizardPages.ActivePage := Page3;
end;

procedure TNewSchemeWizard.acBackwardExecute(Sender: TObject);
begin
  inherited;
  if (WizardPages.ActivePage = Page2)
    or (WizardPages.ActivePage = Page4) then
    WizardPages.ActivePage := Page1
  else if WizardPages.ActivePage = Page3 then
    WizardPages.ActivePage := Page2;
end;

procedure TNewSchemeWizard.lbCS2Click(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  lbConstituents.Clear;
  with TConceptualScheme(lbCS2.Items.Objects[lbCS2.ItemIndex]) do
  begin
    for i := 0 to Pred(ConstituentsCount) do
    begin
      if (Constituents[i].ConstituentType in [ctBaseSet, ctStructure, ctTerm])
        and (Constituents[i].Typification.Width = 1) then
        lbConstituents.AddItem(Constituents[i].CName, Constituents[i]);
    end;
  end;
end;

procedure TNewSchemeWizard.lbCS3Click(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  lbStructures.Clear;
  with TConceptualScheme(lbCS3.Items.Objects[lbCS3.ItemIndex]) do
  begin
    for i := 0 to Pred(ConstituentsCount) do
    begin
      if Constituents[i].ConstituentType = ctStructure then
        lbStructures.AddItem(Constituents[i].CName, Constituents[i]);
    end;
  end;
end;

end.

